﻿using System;
using System.Windows;
using MilitaryMeteo.ViewModels;
using System.Collections.Generic;

namespace MilitaryMeteo
{
    public partial class MainPage
    {
        private static Calculator calculator = new Calculator();

        private InputViewModel inputViewModel;
        private OutputViewModel outputViewModel;

        public MainPage()
        {
            InitializeComponent();
            this.inputViewModel = new InputViewModel();
            this.InputContainer.DataContext = this.inputViewModel;
        }

        private void buttonCalculate_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //test data
                /*calculator.WeaponType = WeaponType.Second;
                calculator.Distance = new int[] { 5000, 7000, 9000 };
                calculator.MeteoData.AlphaOH.Value = 21;
                calculator.MeteoData.Hamc = 208;
                calculator.MeteoData.dhamc = -11;
                calculator.MeteoData.Hop = 69;
                calculator.MeteoData.Tz = -7;
                calculator.MeteoData.dVosum = -2.7;
                int[] meteoNumbers = calculator.GetMeteoGroupNumbers();
                calculator.MeteoGroups = new string[] { "752907", "763008", "763210" };*/
                calculator.MeteoGroups = new [] { inputViewModel.Group1, inputViewModel.Group2, inputViewModel.Group3 };
                List<Result> results = calculator.Calculate();

                this.outputViewModel = new OutputViewModel
                    {
                        D1 = results[0].dDsum.ToString(),
                        D2 = results[1].dDsum.ToString(),
                        D3 = results[2].dDsum.ToString(),
                        A1 = results[0].ddsum.ToString(),
                        A2 = results[1].ddsum.ToString(),
                        A3 = results[2].ddsum.ToString()
                    };

                this.OutputContainer.DataContext = this.outputViewModel;
                this.Pivot.SelectedIndex = 1;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Товарищ студент! Я не понял в чем дело??? " + ex.Message);
            }
        }

        private void buttonGroups_Click(object sender, RoutedEventArgs e)
        {
            calculator.WeaponType = (WeaponType) this.lpWeaponType.SelectedItem;
            calculator.Distance = new[] { (inputViewModel.Distance1 ?? 0) * 1000,
                (inputViewModel.Distance2 ?? 0) * 1000,
                (inputViewModel.Distance3 ?? 0) * 1000 };
            calculator.MeteoData.AlphaOH.Value = inputViewModel.AlphaOH ?? 0;
            calculator.MeteoData.Hamc = inputViewModel.Hamc ?? 0;
            calculator.MeteoData.Hop = inputViewModel.Hop ?? 0;
            calculator.MeteoData.dhamc = inputViewModel.dH0 ?? 0;
            calculator.MeteoData.Tz = inputViewModel.Tz ?? 0;
            calculator.MeteoData.dVosum = inputViewModel.Vsum ?? 0;
            var meteoNumbers = calculator.GetMeteoGroupNumbers();
            tblgroup1.Text = "Группа " + meteoNumbers[0];
            tblgroup2.Text = "Группа " + meteoNumbers[1];
            tblgroup3.Text = "Группа " + meteoNumbers[2];
            this.ScrollViewer.ScrollToVerticalOffset(1000);
            this.tbGroup1.Focus();
        }

        private void TextBox2_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            this.ScrollViewer.ScrollToVerticalOffset(100);
        }

        private void TextBox3_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            this.ScrollViewer.ScrollToVerticalOffset(200);
        }

        private void TextBox4_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            this.ScrollViewer.ScrollToVerticalOffset(300);
        }

        private void TextBox5_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            this.ScrollViewer.ScrollToVerticalOffset(400);
        }

        private void TextBox6_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            this.ScrollViewer.ScrollToVerticalOffset(500);
        }

        private void TextBox7_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            this.ScrollViewer.ScrollToVerticalOffset(580);
        }

        private void TextBox8_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            this.ScrollViewer.ScrollToVerticalOffset(660);
        }

        private void TextBox9_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            this.ScrollViewer.ScrollToVerticalOffset(750);
        }

        private void TextBox10_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            this.ScrollViewer.ScrollToVerticalOffset(1050);
        }

        private void TextBox11_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            this.ScrollViewer.ScrollToVerticalOffset(1130);
        }

        private void TextBox12_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            this.ScrollViewer.ScrollToVerticalOffset(1240);
        }
    }
}